<?php
namespace Home\Model;
use Think\Model;
class SeriveModel extends Model {
    protected $insertFields = array('client_id', 'client_name', 'outline', 'type', 'status', 'ask', 'create_name', 'create_time', 'to_name', 'to_time', 'manage', 'manage_name', 'manage_time', 'manage_fruit', 'manage_myd');
    //protected $updateFields = array('id', 'client_id', 'client_name', 'outline', 'type', 'status', 'ask', 'create_name', 'create_time', 'to_name', 'to_time', 'manage', 'manage_name', 'manage_time', 'manage_fruit', 'manage_myd');
    protected $_validate = array(
        array('type', 'require', '服务类型不能为空！', 1, 'regex', 3),
        array('client_name', 'require', '客户名称不能为空！', 1, 'regex', 3),
        array('outline', 'require', '概要不能为空！', 1, 'regex', 3),
        array('create_time', 'require', '创建时间不能为空！', 1, 'regex', 3),
        array('create_name', 'require', '创建人不能为空！', 1, 'regex', 3),
        array('ask', 'require', '服务请求不能为空！', 1, 'regex', 3),
        array('client_name', '0,30', '客户名称不能超过 30 个字符！', 1, 'length', 3),
        array('outline', '0,100', '概要不能超过 100 个字符！', 1, 'length', 3),
        array('ask', '0,100', '服务请求不能超过 100 个字符！', 1, 'length', 3),
    );
    public $_giveTo = array(
        array('to_name', 'require', '分配给谁不能为空！', 1, 'regex', 7),
    );
    public $yanzheng1 = array(
        array('manage', 'require', '处理方法不能为空！', 1, 'regex', 7),
        array('manage', '1,100', '处理方法最长不能超过 100 个字符！', 1, 'length', 37),
    );
    public $yanzheng2 = array(
        array('manage_fruit', 'require', '处理结果不能为空！', 1, 'regex', 7),
        array('manage_satisfied', 'require', '满意度不能为空！', 1, 'regex', 7),
        array('manage_fruit', '1,200', '处理结果最长不能超过 200 个字符！', 1, 'length', 7),
    );

    public function search($status = NULL, $ismanager = '0', $pageSize = 20) {
        /*********************************************************** 搜索 *************************************** */
        $where = array();
        /*****************************************客户经理限制权限 ************************* */
        if ($ismanager != '0') {

            $sid = session('id');
            $sname = session('username');
            $adModel = M('Admin_role');
            $rid = $adModel->field('role_id')->where(array('admin_id' => array('eq', $sid)))->find();
            if ($rid['role_id'] == '4') {
                $where['to_name'] = array('eq', "$sname");
            }
        }
        $where = array();
        if ($client_name = I('get.client_name'))
            $where['client_name'] = array('like', "%$client_name%");
        if ($outline = I('get.outline'))
            $where['outline'] = array('like', "%$outline%");
        if ($type = I('get.type'))
            $where['type'] = array('eq', $type);
        $stime = I('get.stime');
        $btime = I('get.btime');
        if ($stime && $btime) {
            $stime = strtotime(I('get.stime'));
            $btime = strtotime(I('get.btime'));
            $where['create_time'] = array('between', array($stime, $btime));
        } elseif ($stime) {
            $stime = strtotime(I('get.stime'));
            $btime = strtotime(I('get.btime'));
            $where['create_time'] = array('egt', $stime);
        } elseif ($btime) {
            $stime = strtotime(I('get.stime'));
            $btime = strtotime(I('get.btime'));
            $where['create_time'] = array('elt', $btime);
        }
        if ($status != NUll)
            $where['status'] = array('eq', $status);
        /******************************************************* 翻页 *************************************** */
        $count = $this->alias('a')->where($where)->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $data['page'] = $page->show();
        /******************************************************** 取数据 ***************************************** */
        $data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow . ',' . $page->listRows)->select();
        return $data;
    }

    // 添加前
    protected function _before_insert(&$data, $option) {
        $time = I('post.create_time');
        $time = time();
        //var_dump($time);
        $data['create_time'] = $time;
    }

    // 修改前
    protected function _before_update(&$data, $option) {
        if (I('post.manage_myd') > 2 && I('post.manage_myd')) {
            $data['status'] = '已归档';
        };
        if (I('post.manage_myd') < 3 && I('post.manage_myd')) {
            $data['status'] = '已分配';
        };
    }

    // 删除前
    protected function _before_delete($option) {
        if (is_array($option['where']['id'])) {
            $this->error = '不支持批量删除';
            return FALSE;
        }
    }

    /*************************************************** 其他方法 ******************************************* */

    public function analysisServe($pageSize = 20) {
        /********************************************************* 搜索 *************************************** */
        $where = array();
        if ($_year = I('get.year')) {
            $year = yearstr($_year);
            $where['atime'] = array('gt', $year["first"]);
            $where['atime'] = array('lt', $year["last"]);
        }
        //$oModel=M('order');
        /********************************************* 翻页 *************************************** */
        $count = $this->where($where)->group('type')->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $data['page'] = $page->show();
        /************************************************** 取数据 ***************************************** */

        //$data['data'] = $oModel->field('id,client_id,')->where($where)->group('client_id')->limit($page->firstRow.','.$page->listRows)->select();
        $sql = 'select type,count(type) as number from crm_serive ' . $where1 . '  group by type limit ' . $page->firstRow . ',' . $page->listRows;
        $data['data'] = $this->query($sql);
        return $data;
    }

}
